﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using BEELab.Logic.ProgramManagement.VariableManagement;
using System.Web.Routing;
using Microsoft.AspNet.SignalR;
using System.Threading;

namespace BEELab
{
    public class Global : System.Web.HttpApplication
    {
        public static Dictionary<string, Dictionary<string, Dictionary<string, string>>> MessageBox = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
        public static Dictionary<string, Mutex> MessageBoxMutex = new Dictionary<string, Mutex>();

        public static Dictionary<string, Dictionary<string, VariableManagement.Variable>> SubjectVariables = new Dictionary<string, Dictionary<string, VariableManagement.Variable>>();

        public static Dictionary<int, Dictionary<string, VariableManagement.Variable>> SessionVariables = new Dictionary<int, Dictionary<string, VariableManagement.Variable>>();
        public static Dictionary<int, Dictionary<string, string>> VariableScopeDictionary = new Dictionary<int, Dictionary<string, string>>();

        public static Dictionary<string, string> SubjectConnetionDictionary = new Dictionary<string, string>();

        public static Dictionary<int, Dictionary<int, BEELab.Logic.ProgramManagement.StageManagement.BlockElements.BlockElement>> BlockElementsDictionary = new Dictionary<int,Dictionary<int,Logic.ProgramManagement.StageManagement.BlockElements.BlockElement>>();
        public static Dictionary<int, Dictionary<int, BEELab.Logic.ProgramManagement.StageManagement.PresentationElements.PresentationElement>> PresentationElementsDictionary = new Dictionary<int,Dictionary<int,Logic.ProgramManagement.StageManagement.PresentationElements.PresentationElement>>();
        public static Dictionary<int, Dictionary<int, BEELab.Logic.ProgramManagement.StageManagement.DataElements.DataElement>> DataElementsDictionary = new Dictionary<int,Dictionary<int,Logic.ProgramManagement.StageManagement.DataElements.DataElement>>();
        public static Dictionary<int, Dictionary<int, string>> VariableIDNameDictionary = new Dictionary<int, Dictionary<int, string>>();

        public static HashSet<string> Tokens = new HashSet<string>();

        public static Dictionary<int, DateTime> LastSessionActivity = new Dictionary<int, DateTime>();

        protected void Application_Start(object sender, EventArgs e)
        {
            Logic.Execution.SessionRecovery.RecoverAllSessions();
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Session["ViewStateUserKey"] = Data.CryptographyHelper.GenerateRandomString(256);
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            if (Request.RawUrl.Contains("Main.aspx"))
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                Response.Redirect("Main.aspx");
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {
            Session["ViewStateUserKey"] = "";
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}